/*
This is <BFS> (for basic situations) used to query the cost required for the optimal strategy. 
The update version is <01BFS>, which will apply to more complex graph query strategies. 
now it's BFS 
*/
#include<bits/stdc++.h>
using namespace std;
//there will be multiple images, and the first dimension represents the information of (x, y) in which image. 
//now there is only 1 image (only s[1] and dis[1] are valid)
int n=20,m=40;
int _map=1;//only 1 image
int startx,starty;
struct dian{
	char ch;
	bool vis;
	int dis;
}d[20][105][105];
struct zb{
	int mp,x,y;
};
void init(dian z){
	for(int k=1;k<=_map;k++){
		for(int i=0;i<=n;i++){
			for(int j=0;j<=m;j++){
				d[k][j][i].vis=false;
				if(d[k][j][i].ch=='#') d[k][j][i].dis=-2;
				else d[k][j][i].dis=-1;
			}
		}
	}
}
zb q[200005];
int l=1,r=0;
void bfs(zb st){
	q[++r]=st;
	d[q[1].mp][q[1].x][q[1].y].vis=true;
	d[q[1].mp][q[1].x][q[1].y].dis=0;
	while(l<=r){
		zb now=q[l++];
		zb sr;
		int val=d[now.mp][now.x][now.y].dis;
		//up!......
		sr=now;
		if(d[sr.mp][sr.x][sr.y-1].ch!='#'){
			while(d[sr.mp][sr.x][sr.y-1].ch!='#') sr.y--;
			if(d[sr.mp][sr.x][sr.y].vis==false){
				d[sr.mp][sr.x][sr.y].vis=true;
				d[sr.mp][sr.x][sr.y].dis=val+1;
				q[++r]=sr;
			}
		}
		//down!......
		sr=now;
		if(d[sr.mp][sr.x][sr.y+1].ch!='#'){
			while(d[sr.mp][sr.x][sr.y+1].ch!='#') sr.y++;
			if(d[sr.mp][sr.x][sr.y].vis==false){
				d[sr.mp][sr.x][sr.y].vis=true;
				d[sr.mp][sr.x][sr.y].dis=val+1;
				q[++r]=sr;
			}
		}
		//left!......
		sr=now;
		if(d[sr.mp][sr.x-1][sr.y].ch!='#'){
			while(d[sr.mp][sr.x-1][sr.y].ch!='#') sr.x--;
			if(d[sr.mp][sr.x][sr.y].vis==false){
				d[sr.mp][sr.x][sr.y].vis=true;
				d[sr.mp][sr.x][sr.y].dis=val+1;
				q[++r]=sr;
			}
		}
		//right!......
		sr=now;
		if(d[sr.mp][sr.x+1][sr.y].ch!='#'){
			while(d[sr.mp][sr.x+1][sr.y].ch!='#') sr.x++;
			if(d[sr.mp][sr.x][sr.y].vis==false){
				d[sr.mp][sr.x][sr.y].vis=true;
				d[sr.mp][sr.x][sr.y].dis=val+1;
				q[++r]=sr;
			}
		}
		//.........
	}
}
signed main(){
	freopen("data.out","r",stdin);
	freopen("result.out","w",stdout);
	scanf("%d%d",&startx,&starty);
	for(int i=0;i<=n;i++){
		for(int j=0;j<=m;j++){
			while(d[1][j][i].ch!='#'&&d[1][j][i].ch!='S'&&d[1][j][i].ch!=' ')d[1][j][i].ch=getchar();;
		}
	}
	init({1,startx,starty});
	bfs({1,startx,starty});
	for(int k=1;k<=_map;k++){
		for(int i=0;i<=n;i++){
			for(int j=0;j<=m;j++)
				//cout<<(d[k][j][i].ch!=' '?d[k][j][i].ch:'O')<<' ';
				cout<<d[k][j][i].dis<<' ';
			cout<<'\n';
		}
	}
	fclose(stdin);
    return 0;
}

